<script>
  import uniIdPagesInit from '@/uni_modules/uni-id-pages/init.js';
  import uniIm from '@/uni_modules/uni-im/sdk/index.js';
  import MsgReaderExtension from '@/uni_modules/uni-im-msg-reader/extension.js'
  import { userApi } from '@/http/userApi.js';
  import { USER_TOKEN } from '@/constants/local-storage-key-const'
  import store from './store'
  const consoleTimeObj = {}
  console.time = function (name) {
    consoleTimeObj[name] = Date.now()
  }
  console.timeEnd = function (name,fun) {
    const time = Date.now() - consoleTimeObj[name]
  }
  export default {
    onLaunch: async function() {
      MsgReaderExtension.install()
      uniIdPagesInit();
      uniIm.init();
	  this.setUserInfo()
    },
    onShow: function() {
      console.log('App Show');
    },
    onHide: function() {
      console.log('App Hide');
    },
	methods:{
		setWxHeader(){
			let statusBarHeight = uni.getWindowInfo().statusBarHeight;
			// #ifdef MP 
			let menuButtonHeight = uni.getMenuButtonBoundingClientRect().height;
			let titleBarWidth = uni.getWindowInfo().windowWidth - uni.getMenuButtonBoundingClientRect().width - (uni.getWindowInfo().windowWidth - uni.getMenuButtonBoundingClientRect().right) * 2;
			let titleBarHeight = (uni.getMenuButtonBoundingClientRect().top - statusBarHeight) * 2 + menuButtonHeight;
			store.commit('setWxHeader',{
				'statusBarHeight':statusBarHeight,
				'menuButtonHeight':menuButtonHeight,
				'titleBarWidth':titleBarWidth,
				'titleBarHeight':titleBarHeight
			})
			// #endif
			// #ifdef APP
			store.commit('setStatusBarHeight',statusBarHeight)
			// #endif
		},
		setUserInfo() {
			const token = uni.getStorageSync(USER_TOKEN)
			const uni_id_token = uni.getStorageSync('uni_id_token')
			const uni_id_token_expired = uni.getStorageSync('uni_id_token_expired')
			if(token && uni_id_token && uni_id_token_expired && uni_id_token_expired!=0){
				userApi.getUserInfo().then((res)=>{
					store.commit('setUserInfo',res)
					setTimeout(()=>{
						let unCount = uniIm.conversation.unreadCount()
						// #ifdef APP
						plus.runtime.setBadgeNumber(unCount)
						// #endif
						uniIm.utils.setTabBarBadge(2, unCount)
						// 调用扩展点，更新未读消息总数。
						uniIm.extensions.invokeExts('ui-update-unread-count', unCount)
					},500)
				}).catch((e)=>{
					
				})
			}
		},
	},
  };
</script>

<style lang="scss">
  /*每个页面公共css */
  @import './theme/iconfont.css';
  @import './theme/index.css';
  /* #ifndef APP-NVUE */
  @import "@/uni_modules/uni-im/static/iconfont.css";
  /* #endif */
</style>